package com.lichongbing.lyoggl.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lichongbing.lyoggl.model.Bank;
import com.lichongbing.lyoggl.model.Money;
import com.lichongbing.lyoggl.service.BankService;
import com.lichongbing.lyoggl.utils.JwtTokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.UUID;

/**
 * @author lichongbing
 * @version 1.0.0
 * @createdate 2022/8/11 14:25
 * @description: TODO
 */
@Slf4j
@RestController
@RequestMapping("/bank")
public class BankController {
    @Autowired
    private BankService bankService;

    /**
     * 添加信用卡
     *
     * @param jsonObject
     * @param request
     * @return
     */
    @PostMapping("/create")
    public String createProject(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
        String token = request.getHeader("token");
        boolean expiration = JwtTokenUtils.isExpiration(token);
        Bank project = new Bank();
        String id = UUID.randomUUID().toString().replace("-", "");
        String create = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
        if (expiration == false) {
            String userid = JwtTokenUtils.getUserid(token);
            int type = Integer.parseInt(jsonObject.getString("type"));
            project.setId(id);
            project.setType(type);
            project.setCreateTime(create);
            project.setUid(userid);
            project.setName(jsonObject.getString("name"));
            project.setCard(jsonObject.getString("card"));
            project.setBalance(jsonObject.getString("balance"));
            if(type==0 || type ==2 ){
                project.setDate0(jsonObject.getString("date0"));
                project.setDate1(jsonObject.getString("date1"));
                project.setDuted(jsonObject.getString("duted"));
            }
            project.setDate0("1");
            project.setDate1("1");
            project.setDuted("1");
            project.setUpdateTime("1");
            bankService.save(project);
            return "添加成功";
        } else {
            return "token过期";
        }
    }

    /**
     * 根据用户id查询信用卡
     *
     * @param request
     * @return
     */
    @PostMapping("/findByUid")
    public JSONObject findByUid(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String token = request.getHeader("token");
        boolean expiration = JwtTokenUtils.isExpiration(token);
        if (expiration == false) {
            String userid = JwtTokenUtils.getUserid(token);
            jsonObject.put("code", 200);
            jsonObject.put("msg", "查询成功");
            jsonObject.put("data", bankService.findByUid(userid));
            return jsonObject;
        } else {
            jsonObject.put("code", 400);
            jsonObject.put("msg", "token过期");
            return jsonObject;
        }
    }
    /**
     * 根据id删除信用卡
     *
     * @param id
     * @param request
     * @return
     */
    @PostMapping("/delete/{id}")
    public JSONObject delete(@PathVariable String id, HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String token = request.getHeader("token");
        boolean expiration = JwtTokenUtils.isExpiration(token);
        if (expiration == false) {
            String userid = JwtTokenUtils.getUserid(token);
            bankService.deleteByUid(id, userid);
            jsonObject.put("code", 200);
            jsonObject.put("msg", "删除成功");
            return jsonObject;
        } else {
            jsonObject.put("code", 400);
            jsonObject.put("msg", "token过期");
            return jsonObject;
        }
    }

    /**
     * 银行卡信息更新
     * @param moneys
     * @param request
     * @return
     */
    @PostMapping("/update")
    public JSONObject update(@RequestBody JSONObject moneys, HttpServletRequest request){
        String id = moneys.getString("id");
        String update = DateUtil.format(new Date(), "YYYY-MM-dd HH:mm:ss");
        String name = moneys.getString("name");
        String card = moneys.getString("card");
        String date0 = moneys.getString("date0");
        String date1 = moneys.getString("date1");
        String balance = moneys.getString("balance");
        String duted = moneys.getString("duted");
        UpdateWrapper<Bank> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id).set("update_time",update);
        if (!"".equals(name)) {
            updateWrapper.set("name",name);
        }
        if (!"".equals(card)) {
            updateWrapper.set("card",card);
        }
        if (!"".equals(date0)){
            updateWrapper.set("date0",date0);
        }
        if (!"".equals(date1)){
            updateWrapper.set("date1",date1);
        }
        if (!"".equals(balance)){
            updateWrapper.set("balance",balance);
        }
        if (!"".equals(duted)){
            updateWrapper.set("duted",duted);
        }
        JSONObject res = new JSONObject();
        try {
        boolean isupdate = bankService.update(null, updateWrapper);
        res.put("code", 200);
        res.put("msg",isupdate);
        res.put("data","ok");
        return res;
        }catch(Exception e){
            res.put("code", 400);
            res.put("msg",e.getMessage());
            return res;
        }
    }
}
